﻿<%@ Page Language="C#" AutoEventWireup="true" Inherits="System.Web.UI.Page" %>

<%@ Import Namespace="Portal.Core" %>
<%@ Import Namespace="Portal.Data.Security" %>
<div class="bot-navigation">
    <div class="jumping">
        Đến mục
        <input type="text" id="txtChapterTop" onkeyup="return isNumberKey(event);" style="width: 30px;" />
        <input type="button" id="btnGoChapTop" value="Xem"/>
        | Đến trang
        <select id="goPageTop" onchange="goPageBook(<%= BookId %>,this.options[this.selectedIndex].value,'#bookchaps');">
        </select>
    </div>
    <br class="clear" />
    <asp:placeholder runat="server" id="phPagingTop"></asp:placeholder>
</div>
<br class="clear" />
<asp:repeater id="ListItem" runat="server">
    <HeaderTemplate>
        <ul class="list">
    </HeaderTemplate>
    <ItemTemplate>
        <li>
            <div class="little left blue bold">
                <a href="<%#Eval("Url")%>">--<%#Eval("Chapter")%>-- </a>
            </div>
            <div class="title left">
                <a href="<%#Eval("Url")%>"><%#Eval("Title")%></a>
            </div>
        </li>
    </ItemTemplate>
    <FooterTemplate>
        </ul>
    </FooterTemplate>
</asp:repeater>
<div class="bot-navigation">
    <div class="jumping">
        Đến mục
        <input type="text" id="txtChapter" onkeyup="return isNumberKey(event);" style="width: 30px;" />
        <input type="button" id="btnViewChap" value="Xem"/>
        | Đến trang
        <select id="goPageBottom" onchange="goPageBook(<%= BookId %>,this.options[this.selectedIndex].value,'#bookchaps');">
        </select>
    </div>
    <asp:placeholder runat="server" id="phPagingBottom"></asp:placeholder>
</div>
<script type="text/javascript">
    jQuery(function ($) {
        var bid = '<%= BookId %>' ;
        //bind to select page
        var maxpage = '<%= Maxpage %>';
        var curPage = parseInt(<%= Request["page"] %>);        
        for (var i = 1; i <= maxpage; i++) {
            var item = '<option value="'+i+'">'+i+'</option>';
            if(curPage==i)
                item = '<option selected="selected" value="'+i+'">'+i+'</option>';
            $('#goPageTop').append(item);
            $('#goPageBottom').append(item);
        }
        
        $('#txtChapter').keyup(function (e) {
            var key = e.keyCode || e.which;
            if (key === 13) {
                goChap(bid,$(this).val());
            }
        });
        $('#txtChapterTop').keyup(function (e) {
            var key = e.keyCode || e.which;
            if (key === 13) {
                goChap(bid,$(this).val());
            }
        });
        $('#btnGoChapTop').click(function () {
            goChap(bid,$('#txtChapterTop').val());
        });
        $('#btnViewChap').click(function () {
            goChap(bid,$('#txtChapter').val());
        });
    });
</script>

<script runat="server">
    public static readonly int MAX50 = 50;
    public int BookId, Maxpage;
    //public string HtmlCache = string.Empty, KeyUnique;
    //public bool ResetCache;
    protected override void OnInit(EventArgs e)
    {
        if (!SecurityProvider.AllowCall())
            return;
//        KeyUnique = Portal.Data.PortalContext.PrefixWeb + Request.RawUrl.GetHashCode();
//        //get from html cache
//        var obj = CacheHelper.Instance.Get(KeyUnique);
//        if (obj != null)
//        {
//            HtmlCache = obj.ToString();
//            return;
//        }
//        ResetCache = true;
        
        int.TryParse(Request["bid"], out BookId);
        int page, pagesize;
        int.TryParse(Request["page"], out page);
        if (BookId <= 0) return;

        int.TryParse(Request["pagesize"], out pagesize);
        if (pagesize < 0) pagesize = 1;
        if (pagesize == 0 || pagesize > MAX50) pagesize = MAX50;

        int count;
        var virtalPage = page - 1;
        if (virtalPage < 0) virtalPage = 0;

        //var books = ChapInfoHelper.Instance.Paging(BookId, pagesize, page, out count);
        var books = Portal.Data.Entity.Content.Paging(BookId, pagesize, virtalPage, out count);
        Maxpage = count / pagesize;
        if (count % pagesize != 0)
            Maxpage += 1;

        if (books.Count == 0)
        {
            Response.Write("");
            Response.End();
        }
        else
        {
            ListItem.DataSource = books;
            ListItem.DataBind();
            Common.AddPagingToolAjax(new Control[] { phPagingTop, phPagingBottom }, count, pagesize, "#bookchaps");
        }
    }
//    protected override void Render(HtmlTextWriter writer)
//    {
//        var sbOut = new StringBuilder();
//        var swOut = new StringWriter(sbOut);
//        var htwOut = new HtmlTextWriter(swOut);
//        base.Render(htwOut);
//        string sOut = sbOut.ToString();
//        if (ResetCache)
//        {
//            CacheHelper.Instance.Set(KeyUnique, sOut, Portal.Data.PortalContext.HtmlCachingMinute);
//        }
//        writer.Write(sOut);
//    }
</script>
